package com.dinggou.shopjb.view;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.dinggou.shopjb.R;
import com.dinggou.shopjb.base.BaseActivity;
import com.dinggou.shopjb.bean.UserInfoBean;
import com.dinggou.shopjb.utils.TimeCount;


public class LogBackSetPwdDialog extends MyDialog implements View.OnClickListener {
    private EditText et_password;
    private EditText et_code;
    private TextView tv_get_code;
    private DialogListener dialogListener;
    private Dialog mLoadingDialog;
    private Context mContext;
    private String pushId = "";
    private String mobile;
//    private AppAction mAppAction;
    private TimeCount timeCount;
    private BaseActivity mBaseActivity;

    public LogBackSetPwdDialog(Context context, DialogListener dialogListener) {
        super(context, false, Gravity.BOTTOM);
        mContext = context;
        this.dialogListener = dialogListener;
        View rootView = LayoutInflater.from(context).inflate(R.layout.dialog_login_set_pwd, null);
        et_code = rootView.findViewById(R.id.et_code);
        et_password = rootView.findViewById(R.id.et_password);
        tv_get_code = rootView.findViewById(R.id.tv_get_code);
        TextView tv_mobile = rootView.findViewById(R.id.tv_mobile);
        Button bt_cancel = rootView.findViewById(R.id.bt_cancel);
        Button bt_login = rootView.findViewById(R.id.bt_login);
        tv_get_code.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        bt_login.setOnClickListener(this);
        mBaseActivity = (BaseActivity) mContext;
//        mAppAction = mBaseActivity.mAppAction;
//        mobile = DataUtils.getMobile(mContext);
//        tv_mobile.setText("当前账号：" + CertificateModule.getMobileHide(mobile));
        setContentView(rootView);

    }

    @Override
    public void onClick(View v) {
//        if (FastClickUtil.isFastClick()) {
//            return;
//        }
//        switch (v.getId()) {
//            case R.id.tv_get_code:
//                getCode();
//                break;
//            case R.id.bt_cancel:
//                dismiss();
//                dialogListener.onCancel();
//                break;
//            case R.id.bt_login:
//                submit();
////                login();
//                break;
//        }
    }

    private void getCode() {
//        if (mLoadingDialog == null) {
//            mLoadingDialog = DialogLoading.createLoadingDialog(mBaseActivity, "", false);
//        }else{
//            mLoadingDialog.show();
//        }
//        mAppAction.resetPasswordCode(mobile, new ActionCallbackListener<Void>() {
//            @Override
//            public void onSuccess(Void data) {
//                if (mLoadingDialog != null) {
//                    DialogLoading.cancelLoadingDialog(mLoadingDialog);
//                }
//                timeCount = new TimeCount(tv_get_code, 60000, 1000, "再次获取");
//                timeCount.start();
//                ToastUtil.showToast(mContext, "验证码发送成功");
//            }
//
//            @Override
//            public void onFailure(String errorCode, String message) {
//                if (mLoadingDialog != null) {
//                    DialogLoading.cancelLoadingDialog(mLoadingDialog);
//                }
//                ToastUtil.showToast(mContext, message);
//            }
//        });
    }

    private void submit() {
//        String code = et_code.getText().toString();
//        String password = et_password.getText().toString();
//        if(TextUtils.isEmpty(code)){
//            ToastUtil.showToast(mContext,"请输入验证码");
//            return;
//        }
//        if(TextUtils.isEmpty(password)){
//            ToastUtil.showToast(mContext,"请输入密码");
//            return;
//        }
//        if (mLoadingDialog == null) {
//            mLoadingDialog = DialogLoading.createLoadingDialog(mBaseActivity, "", false);
//        }else{
//            mLoadingDialog.show();
//        }
//        mAppAction.resetPassword(mobile, code, password,
//                new ActionCallbackListener<Void>() {
//                    @Override
//                    public void onSuccess(Void data) {
//                        login();
//                    }
//
//                    @Override
//                    public void onFailure(String errorCode, String message) {
//                        if (mLoadingDialog != null) {
//                            DialogLoading.cancelLoadingDialog(mLoadingDialog);
//                        }
//                        ToastUtil.showToast(mContext, message);
//                    }
//                });
    }

    private void login() {
//        if (mLoadingDialog == null) {
//            mLoadingDialog = DialogLoading.createLoadingDialog(mBaseActivity, "", false);
//        }else{
//            mLoadingDialog.show();
//        }
//        mAppAction.login(mobile, et_password.getText().toString().trim(), pushId, new ActionCallbackListener<UserInfoBean>() {
//            @Override
//            public void onSuccess(UserInfoBean data) {
//                ToastUtil.showToast(mContext,"登录成功");
//                if (mLoadingDialog != null) {
//                    DialogLoading.cancelLoadingDialog(mLoadingDialog);
//                }
//                DataUtils.setRegister(getContext(), true);
//                DataUtils.setLogin(getContext(), true);
//                DataUtils.setUserInfo(getContext(), data);
//                if ("1".equals(data.getFirst_pay())) {
//                    DataUtils.setHasNoviceTicket(getContext(), true);
//                    mBaseActivity.sendBroadcast(new Intent(MyApplication.GUIDE_NOVICE_TICKET_ACTION));
//                } else {
//                    DataUtils.setHasNoviceTicket(getContext(), false);
//                }
//                dialogListener.onSuccess(data);
//                dismiss();
//            }
//
//            @Override
//            public void onFailure(String errorCode, String message) {
//                if (mLoadingDialog != null) {
//                    DialogLoading.cancelLoadingDialog(mLoadingDialog);
//                }
//                ToastUtil.showToast(getContext(), message);
//            }
//        });
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (timeCount != null) {
            timeCount.finishTimeCount();
            timeCount = null;
        }
    }

    public interface DialogListener {
        void onCancel();

        void onSuccess(UserInfoBean userInfoBean);
    }
}
